package com.xuecheng.manage_cms.web.controller;

import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_cms.service.PageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import java.io.IOException;

@Controller
@Api(value="CMS预览网页",description="cms预览模板网页")
public class CmsPagePreviewController extends BaseController {
    @Autowired
    private PageService pageService;

    /**
     * 根据pageid 预览模板网页
     * @param id
     */
    @GetMapping("/cms/preview/{id}")
    @ApiOperation("预览方法通过pageid")
    @ApiImplicitParam(name = "pageId" ,value = "页面id",dataType = "java.lang.String")
    public void preview(@PathVariable("id") String id){
        String pageHtml = pageService.getPageHtml(id);

        if(StringUtils.isNotEmpty(pageHtml)){
            try {
                ServletOutputStream outputStream = response.getOutputStream();
                outputStream.write(pageHtml.getBytes("utf-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
